/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.staf.NetworkReportGenerator.V2;

import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.Set;
import java.util.HashSet;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Counter32;
import org.snmp4j.smi.Counter64;
import org.snmp4j.smi.Gauge32;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.IpAddress;
import org.snmp4j.smi.TimeTicks;

import java.io.FileNotFoundException;
import java.io.IOException;

import ru.staf.NetworkReportGenerator.SysUtil;

/**
 *
 * @author aezhov
 */
public class FileSnmpWorker implements SnmpWorker {

    File snmpDump = null;

    public FileSnmpWorker(File snmpDump) {
        this.snmpDump = snmpDump;
    }

    public boolean setSnmpVersion(int version) {
        return true;
    }

    public boolean setSnmpCommunity(String community) {
        return true;
    }

    public Set<VariableBinding> snmpWalk(String oidString)
            throws SnmpException {
        HashSet<VariableBinding> result = new HashSet<VariableBinding>();
        LineNumberReader reader = null;
        oidString = oidString.replaceAll("\\.", "\\.");
        Pattern p = Pattern.compile("^(" + oidString + "\\.[.0-9]+)\\s+=\\s+([A-Za-z0-9-]+):\\s+(.+)$");

        try {
            reader = new LineNumberReader(new FileReader(snmpDump));

            String line = null;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith(oidString)) {
                    Matcher m = p.matcher(line);
                    if (m.matches()) {
                        String oidStr = m.group(1);
                        String type = m.group(2).toLowerCase();
                        String valueStr = preProcess(m.group(3), type);
                        Variable var = null;
                        OID oid = new OID(oidStr);

                        if (type.equals("counter32")) {
                            Long value = new Long(valueStr);
                            var = new Counter32(value);
                        } else if (type.equals("counter64")) {
                            Long value = new Long(valueStr);
                            var = new Counter64(value);
                        } else if (type.equals("gauge32")) {
                            Long value = new Long(valueStr);
                            var = new Counter64(value);
                        } else if (type.equals("hex-string")) {
                            String[] bytes = valueStr.split("\\s+");
                            byte[] rawData = new byte[bytes.length];
                            int i = 0;
                            for (String abyte : bytes) {
                                int irb = Integer.parseInt(abyte, 16);
                                rawData[i++] = (byte) irb;
                            }
                            var = new OctetString(rawData);
                        } else if (type.equals("integer")) {
                            Integer value = new Integer(valueStr);
                            var = new Integer32(value);
                        } else if (type.equals("ipaddress")) {
                            var = new IpAddress(valueStr);
                        } else if (type.equals("oid")) {
                            var = new OID(valueStr);
                        } else if (type.equals("string")) {
                            var = new OctetString(valueStr);
                        } else if (type.equals("timeticks")) {
                            Long value = new Long(valueStr);
                            var = new TimeTicks(value);
                        }

                        VariableBinding vb = new VariableBinding(oid, var);
                        result.add(vb);
                    }
                }
            }

        } catch (FileNotFoundException exc) {
            throw new SnmpException("FileSnmpWorker. File "
                    + snmpDump.getName() + " not found");
        } catch (IOException exc) {
            throw new SnmpException("FileSnmpWorker. IO Exception for"
                    + " file " + snmpDump.getName() + " " + exc);
        }

        return result;
    }

    String preProcess(String data, String type) {
        String result = null;

        // Eliminate brackets
        if (type.equals("integer") || type.equals("counter32")
                || type.equals("counter64") || type.equals("gauge32") || type.equals("timeticks")) {
            int lbi = data.indexOf('(');
            if (lbi != -1) {
                int rbi = data.indexOf(')');
                result = data.substring(lbi + 1, rbi).replaceAll("[^0-9]", "");
            } else {
                result = data.replaceAll("[^0-9]", "");
            }
        } else {
            return data;
        }

        return result;
    }
}
